package lab2Exercise3;

public class ArrayStack<E> implements Stack<E> {
	private E[] S;
	private int top;
	private int capacity;
	
	private static int DEFAULT_SIZE = 100;
	
	public ArrayStack(int size){
		capacity = size;
		S = (E[]) new Object[size];
		top = -1;
	}
	
	public ArrayStack(){
		this(DEFAULT_SIZE);
	}
	
	public boolean isEmpty()
	{
		return (top < 0);
	}
	
	
	public E pop() throws StackException{
		if(isEmpty())
			throw new StackException("stack is empty");
		return S[top--];
	}
	
	public E top() throws StackException 
	{
		if(isEmpty()) {
			throw new StackException("stack is empty");
		}
		return S[top];
	}
	
	public void push(E e)
	{
		if(size() == capacity) {
			E[] tmp = (E[]) new Object[2*size()];
			for (int i = 0; i < S.length; i++){
				tmp[i] = S[i];
			}
			S = tmp;
			setCapacity(size());
			
		}
		S[++top] = e;
	}
	
	public int size()
	{
		return top+1;
	}
	
	private void setCapacity(int size) 
	{
		capacity = size;
	}

}